/**  
* Copyright (c) 2009, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// created by noellynch
// Nov 9, 2009

package com.sceneworker.monitorpopup.panels.importers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.jme.scene.Controller;
import com.jme2utils.importers.FormatConverterImportCallback;
import com.jme2utils.staticutils.ModelUtils;
import com.jmex.model.converters.FormatConverter;
import com.model.md5.MD5Node;
import com.model.md5.importer.MD5Importer;
import com.myjavautils.GenericUtils;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SimpleAlert;
import com.myjavautils.swing.SwingComponentFactory;

public class MD5Import extends FormatConverterImportCallback {

	private		Object			mcl_par;
	
	public MD5Import() {
	}

	public MD5Import(FormatConverter cl_conv) {
		super(cl_conv);
	}

	@Override
	public boolean doImportUI(Object cl_parentObject) {
		mcl_par = cl_parentObject;
		
		JDialog		lcl_dlg =  new JDialog();
		lcl_dlg.setModal(true);
		lcl_dlg.add(new MD5ImportPanel(lcl_dlg));
		lcl_dlg.pack();
		lcl_dlg.setLocationRelativeTo(null);
		lcl_dlg.setVisible(true);
		
		return true;
	}
	
	public	class	MD5ImportPanel extends JPanel {
		private		File		mcl_meshFile;
		private		File		mcl_animFile;
		
		public	MD5ImportPanel(final JDialog cl_par) {
			MigLayoutUtils.asMigLayout(this);
			
			buildMeshImportPanel(this);
			
			JButton			lcl_cancel = SwingComponentFactory.makeAButton("Cancel");
			lcl_cancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					cl_par.dispose();
				}
			});
			
			JButton			lcl_import = SwingComponentFactory.makeAButton("Import");
			lcl_import.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					loadMD5();
					cl_par.dispose();
				}
			});
			
			MigLayoutUtils.makeRightAlignedButtonBar(this, lcl_import, lcl_cancel);
		}
		
		private		void		buildMeshImportPanel(JPanel cl_par) {
			JButton					lcl_locateMeshBtn = SwingComponentFactory.makeAButton("Locate Mesh");
			final	JLabel			lcl_locateMeshLabel = SwingComponentFactory.makeALabel("mesh.md5");
			lcl_locateMeshBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					File	lcl_f = GenericUtils.getAFile(null, false, false);
					if(lcl_f == null) { return; }
					
					mcl_meshFile = lcl_f;
					lcl_locateMeshLabel.setText(mcl_meshFile.getName());
					lcl_locateMeshLabel.repaint();
				}
			});
			cl_par.add(lcl_locateMeshBtn);
			cl_par.add(lcl_locateMeshLabel, "wrap");
			
			JButton					lcl_locateAnimBtn = SwingComponentFactory.makeAButton("Locate Animation");
			final	JLabel			lcl_locateAnimLabel = SwingComponentFactory.makeALabel("anim.md5");
			lcl_locateAnimBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					File	lcl_f = GenericUtils.getAFile(null, false, false);
					if(lcl_f == null) { return; }
					
					mcl_animFile = lcl_f;
					lcl_locateAnimLabel.setText(mcl_animFile.getName());
					lcl_locateAnimLabel.repaint();
				}
			});
			
			cl_par.add(lcl_locateAnimBtn);
			cl_par.add(lcl_locateAnimLabel, "wrap");
		}
		
		private		void	loadMD5() {
			MD5Importer lcl_importer = new MD5Importer();
			try {
				lcl_importer.load(mcl_meshFile.toURI().toURL(), "ImportedMD5Mesh", mcl_animFile.toURI().toURL(), "ImportedMD5Controller", Controller.RT_WRAP);

				MD5Node lcl_body = (MD5Node) lcl_importer.getMD5Node();
				ModelUtils.handleLoadedObject(lcl_body, mcl_par);
				
				lcl_importer.cleanup();
			} catch (Exception e) {
				SimpleAlert.doAlert("Error occured loading MD5 File " + e.getMessage(), this);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

}